using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NiSis.Data;
using NiSis.Entities;


namespace NiSis.UserControls
{
    /// <summary>
    /// Visualizza l'elenco dei posibile shipper da inserire come relazioni in un batch, contenuto dal controllo
    /// RelationshipPicker
    /// </summary>
    public partial class ShipperRel : System.Web.UI.UserControl
    {
        private VList<VShipperAttiviPool> mShippers;

        public VList<VShipperAttiviPool> Shippers
        {
            get { return mShippers; }
            set { mShippers = value; }
        }
        protected string CurrentPool
        {
            get
            {
                return hdCdPool.Value;
            }
        }

        public bool CheckForRed
        {
            get
            {
                return hdCheckForRed.Value == "y";
            }
            set
            {
                if (value)
                    hdCheckForRed.Value = "y";
                else
                    hdCheckForRed.Value = string.Empty;
            }
        }

        public string CurrentCdTraderPrecedente
        {
            get
            {
                return hdCdTraderPrecedente.Value;
            }
            set
            {
                hdCdTraderPrecedente.Value = value;
            }
        }

        protected string CurrentCdStoricoMensileRete
        {
            get
            {
                return hdCdStoricoMensileRete.Value;
            }
        }
	
        protected void Page_Load(object sender, EventArgs e)
        {
            SetListColors();
            if (!Page.IsPostBack)
            {
                //Shippers = DataRepository.VShipperAttiviPoolProvider.GetAll();
                //Refresh();
            }

        }
        public void LoadShippers(string cdPool, string CdStoricoMensileRete,string cdTraderPrec)
        {
            CurrentCdTraderPrecedente = cdTraderPrec;
            hdCdStoricoMensileRete.Value = CdStoricoMensileRete;
            hdCdPool.Value = cdPool;
            ddlOpzioni.SelectedIndex = 0;

            VList<IShipperTraderPool> shippers = PopolateShippersList();
            //PnlPools.Visible = shippers.Count > 0;
            lstShippers.DataSource = shippers;
            lstShippers.DataBind();
            SetListColors();

        }
        protected void Refresh()
        {
            lstShippers.DataSource = mShippers;
            lstShippers.DataBind();
            SetListColors();
        }
      
        protected VList<IShipperTraderPool> PopolateShippersList()
        {
            hdpiva.Value = string.Empty;

            if (string.IsNullOrEmpty(CurrentPool) || CurrentCdTraderPrecedente==string.Empty)
            {
                return new VList<IShipperTraderPool>();
                
            }
            string strWhere = string.Empty;
            MonthCalculator oCalc = new MonthCalculator();
            oCalc.SetDate(CurrentCdStoricoMensileRete);

            switch (ddlOpzioni.SelectedValue)
            {
                case "Mese":
                    strWhere = string.Format(
                        "cd_pool='{0}' and CD_STORICO_MENSILE_RETE='{1}' and tipologia_societa in ('shipper','New Shipper') and cd_trader_precedente in ({2})",
                        CurrentPool, oCalc.GetMeseM1(),CurrentCdTraderPrecedente);
                    return PopolateShippersList(strWhere,
                        string.Format("cd_pool='{0}' and cd_storico_mensile_rete='{1}'", CurrentPool, oCalc.GetMeseM1()));
                    break;
                case "Mese12":

                    strWhere = String.Format(
                        "cd_pool='{0}' and CD_STORICO_MENSILE_RETE in ('{1}','{2}') and tipologia_societa in ('shipper','New Shipper') and cd_trader_precedente in ({3})",
                        CurrentPool, oCalc.GetMeseM2(), oCalc.GetMeseM3(), CurrentCdTraderPrecedente);
                    return PopolateShippersList(strWhere,
                        string.Format("cd_pool='{0}' and cd_storico_mensile_rete in ('{1}','{2}')", CurrentPool, oCalc.GetMeseM2(),oCalc.GetMeseM3()));
                    break;
                case "Relazioni":
                    return DataRepositoryUtils.GetShipperDaRelazioniSocietarie(CurrentPool, CurrentCdStoricoMensileRete,CurrentCdTraderPrecedente);
                    break;
                case "AltriPool":
                    strWhere = string.Format("cd_pool<>'{0}' and CD_STORICO_MENSILE_RETE='{1}' and cd_trader_precedente in ({2})", CurrentPool, oCalc.GetMeseM1(), CurrentCdTraderPrecedente);
                    VList<IShipperTraderPool> oldSoc= PopolateShippersList(strWhere,
                        string.Format("cd_pool<>'{0}' and CD_STORICO_MENSILE_RETE='{1}'",CurrentPool, oCalc.GetMeseM1()));
                    //VList<IShipperTraderPool> newSoc = DataRepositoryUtils.GetFilteredNewShippers(
                    //    string.Format("cd_pool<>'{0}' and CD_STORICO_MENSILE_RETE='{1}'", CurrentPool, CurrentCdStoricoMensileRete));

                    //foreach (IShipperTraderPool s in newSoc)
                    //    oldSoc.Add(s);

                    oldSoc.Sort("DsSocieta");
                    return oldSoc;
                    break;
                case "Default":
                    return DataRepositoryUtils.GetAllShippers(CurrentPool, CurrentCdStoricoMensileRete,"ds_societa");
                case "All":
                    strWhere = String.Format(
                        "cd_pool='{0}' and CD_STORICO_MENSILE_RETE not in ('{1}','{2}','{4}') and cd_trader_precedente not in ({3})  and tipologia_societa in ('shipper','New Shipper')",
                        CurrentPool, oCalc.GetMeseM2(), oCalc.GetMeseM3(), CurrentCdTraderPrecedente,oCalc.GetMeseM1());
                    return PopolateShippersListAll(strWhere, string.Format("cd_pool='{0}' and CD_STORICO_MENSILE_RETE not in ('{1}','{2}','{3}')", CurrentPool, oCalc.GetMeseM1(), oCalc.GetMeseM2(), oCalc.GetMeseM2()));
                    break;
                case "Test":
                    VList<VSimpleShipperFilter> fakes = new VList<VSimpleShipperFilter>();
                    VSimpleShipperFilter fake = new VSimpleShipperFilter();
                    fake.CdPool = CurrentPool;
                    fake.CdSocieta="0013743";
                    fake.CdStoricoMensileRete="200812";
                    fake.CdTraderPrecedente=CurrentCdTraderPrecedente;
                    fake.DsSocieta = "Lumetec S.p.A.";

                    fakes.Add(fake);

                    fake = new VSimpleShipperFilter();
                    fake.CdPool = CurrentPool;
                    fake.CdSocieta = "0069485";
                    fake.CdStoricoMensileRete="200812";
                    fake.CdTraderPrecedente=CurrentCdTraderPrecedente;
                    fake.DsSocieta = "Benaco Gas Srl";
                    fakes.Add(fake);

                    VList<IShipperTraderPool> results=new VList<IShipperTraderPool>();
                    
                    foreach (VSimpleShipperFilter sp in fakes)
                        results.Add((IShipperTraderPool) sp);

                    return results;
                    

                default:
                    return null;
                    break;
            }

            
           
        }
        protected void SetListColors()
        {
            if (!CheckForRed)
                return;

            if (lstShippers.Items.Count == 0)
                return;
            string sQuery;
            if (CurrentCdTraderPrecedente != string.Empty)
                sQuery = string.Format("cd_pool='{0}' and cd_storico_mensile_rete='{1}' and cd_trader_precedente in ({2})"
                    , CurrentPool, CurrentCdStoricoMensileRete, CurrentCdTraderPrecedente);
            else
                sQuery = string.Format("cd_pool='{0}' and cd_storico_mensile_rete='{1}'",
                    CurrentPool, CurrentCdStoricoMensileRete);
            VList<VRejectedShippers> sh =DataRepository.VRejectedShippersProvider.Get(sQuery,string.Empty);
            if (sh.Count == 0)
                return;

            foreach (ListItem li in lstShippers.Items)
                if (sh.Find(VRejectedShippersColumn.RagioneSociale, li.Value) != null)
                    li.Attributes.CssStyle.Add("COLOR", "#FF0000");

        }
      
      
        protected VList<IShipperTraderPool> PopolateShippersList(string whereString,string sWhereD)
        {
            VList<IShipperTraderPool> sh = DataRepositoryUtils.GetFilteredShippers(whereString, "ds_societa",sWhereD);
            
            
            return sh;
        }
        protected VList<IShipperTraderPool> PopolateShippersListAll(string whereString, string sWhereD)
        {
            VList<IShipperTraderPool> sh = DataRepositoryUtils.GetFilteredShippersAll(whereString, "ds_societa", sWhereD,CurrentCdStoricoMensileRete);

            return sh;
        }
        public IShipperTraderPool GetSelectedShipper(string cdPool, string CdStoricoMensileRete)
        {
            VShipperAttiviPool selShipper = new VShipperAttiviPool();
            ListItem li = lstShippers.SelectedItem;
            if (li == null)
                return null;
            if (hdpiva.Value != string.Empty)
            {
                selShipper.Piva = hdpiva.Value;
                selShipper.CdSocieta = li.Value ?? string.Empty;
                selShipper.DsSocieta = li.Text;
                selShipper.CdPool = CurrentPool;
                selShipper.CdStoricoMensileRete = hdCdMese.Value;
                return (IShipperTraderPool) selShipper;
            }
            return DataRepositoryUtils.GetShipperPoolbyDsSocieta(li.Value ?? string.Empty);
            
        }
      
        protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddlOpzioni_SelectedIndexChanged1(object sender, EventArgs e)
        {
            VList<IShipperTraderPool> shippers = PopolateShippersList();
            //pnlTraders.Visible = shippers.Count > 0;
            lstShippers.DataSource = shippers;
            lstShippers.DataBind();
            SetListColors();
        }
    }

    
}